layui.use(['form', 'table','layuimini','element', 'laydate'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini,
        laydate = layui.laydate;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoSeeBook = {
        id: 'BoSeeBookTable'
    };

    var column = [
        [
            {type: 'numbers'},
            {field: 'userName', title: '用户名', align: 'center'},
            {field: 'bookName', title: '书名', align: 'center'},
            {field: 'directoryName', title: '目录名', align: 'center'},
            {field: 'bookListName', title: '章节名', align: 'center'},
            {field: 'startTime', title: '开始时间', align: 'center'},
            {field: 'endTime', title: '结束时间', align: 'center'},
            {field: 'status', title: '状态', align: 'center',
                templet: function(object){
                    if(0 == object.status){
                        return '<span class="layui-badge">未开始</span>';
                    }else if(1 == object.status){
                        return '<span class="layui-badge layui-bg-green">已开始</span>';
                    }else if(2 == object.status){
                        return '<span class="layui-badge layui-bg-cyan">已结束</span>';
                    }else if(3 == object.status){
                        return '<span class="layui-badge layui-bg-gray">已放弃</span>';
                    }
                }
            },
            {field: 'sweet', title: '亲密值', align: 'center',
                templet: function(object){
                    return '<span style="color: #FF5722;">' + object.sweet + '</span>';
                }
            },
            {field: 'reson', title: '放弃原因', align: 'center',
                templet: function(object){
                    if(object.reson == '' || object.reson == null || object.reson == undefined){
                        return '-';
                    }else{
                        return object.reson;
                    }
                }
            },
            {field: 'createDate', title: '时间', align: 'center'},
            {templet: '#stateBtn', width: 140, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + BoSeeBook.id,
        url: '/web/boSeeBook/list',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    // 监听搜索操作
    $("#data-search-btn").on('click', function () {

        //获取搜索条件
        var userName = $("#search_userName").val();
        var status = $("#search_status").val();
        var bookName = $("#search_bookName").val();

        //封装
        var dataObject = {};
        dataObject['userName'] = userName;
        dataObject['status'] = status;
        dataObject['bookName'] = bookName;
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoSeeBook.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听重置操作
    $("#data-reset-btn").on('click', function () {

        //初始化
        $("#search_userName").val("");
        $("#search_status").val("-1");
        $("#search_bookName").val("");

        form.render();

        //封装
        var dataObject = {};
        dataObject['userName'] = "";
        dataObject['status'] = "-1";
        dataObject['bookName'] = "";
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoSeeBook.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    /**
     * 点击编辑按钮
     * @param id
     */
    BoSeeBook.edit = function(id){
        var content = layuimini.getHrefContent('/web/boSeeBook/addOrEdit?id=' + id);
        var index = layer.open({
            title: '看书清单详情',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            zIndex: 998,
            area: ['700px','650px'], //宽高
            content: content,
            // end: function(){
            //     //刷新表格
            //     table.reload(BoSeeBook.id);
            // }
        });

        return false;
    };

    /**
     * 点击删除按钮
     * @param id
     */
    BoSeeBook.delete = function(id){
        layer.open({
            title: '删除看书清单'
            ,content: '是否确定删除该看书清单？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boSeeBook/delete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoSeeBook.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };


    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + BoSeeBook.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            BoSeeBook.delete(data.id);
        }else if(layEvent === 'edit'){
            BoSeeBook.edit(data.id);
        }
    });

});